package com.herench.bs.handler;

import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.dao.IClinicChargeRecordDao;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Handler(code = "102100" ,name="获取诊间缴费信息（全部）")
public class LoadAllClinicOrderListHandler extends AbstractMessageHandler {
    @Autowired
    private IClinicChargeRecordDao clinicChargeRecordDao;

    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        String recordId = requestMessage.getArgsPropertyStringValue("recordId");
        Assert.notNull(recordId, "recordId 不可为空！");
        String patientId = requestMessage.getArgsPropertyStringValue("patientId");
        Assert.notNull(patientId, "patientId 不可为空！");
        String source = requestMessage.getArgsPropertyStringValue("source");
        String user = requestMessage.getArgsPropertyStringValue("user");
        Assert.notNull(user, "user 不可为空！");
        String payState = requestMessage.getArgsPropertyStringValue("payState");
        String clinicPayType = requestMessage.getArgsPropertyStringValue("clinicPayType");
        Map<String, Object> res = new HashMap<>(16);
        String hospitalCode = getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
        IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
        Map<String, Object> charge = new HashMap<>(16);
        charge.put("patientId",patientId);
        charge.put("clinicChargeStatus","2");
        charge.put("channelCode",channelCode);
        charge.put("hospitalCode",hospitalCode);
        List<Map<String, Object>> resChargeList = new ArrayList<>();
        resChargeList = clinicChargeRecordDao.find(charge);
        if (resChargeList.size()>0){
            recordId = (String) resChargeList.get(0).get("recordId") ;
          String  paySeq = (String) resChargeList.get(0).get("paySeq") ;
//            Map<String, Object> resPatientId = outpatientClinicAgent.queryPatientInfo(channelCode, hospitalCode, idCard);
//            String patientId = (String) resPatientId.get("patientId");
            Map<String, Object> resCharge = new HashMap<>(16);
            resCharge = outpatientClinicAgent.canceleClinicChargePaymentInsurance(channelCode, hospitalCode, recordId, patientId, paySeq);
        }

//        IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
//        String hospitalCode = getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
        res = outpatientClinicAgent.loadAllClinicOrderList(channelCode, hospitalCode,recordId,patientId,source,user,payState,clinicPayType);
        responseMessage.setData(res);
        getPropertiesFromInvokerResultMap(responseMessage, res);
        return responseMessage;
    }
}
